package com.yunhai.web.controller.news;

import java.util.List;
import java.util.Arrays;

import com.yunhai.system.service.NewsService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yunhai.common.annotation.Log;
import com.yunhai.common.core.controller.BaseController;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.enums.BusinessType;
import com.yunhai.system.domain.News;
import com.yunhai.common.utils.poi.ExcelUtil;
import com.yunhai.common.core.page.TableDataInfo;

/**
 * 资讯Controller
 *
 * @author jxj
 * @date 2021-01-16
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/news/news")
public class NewsController extends BaseController {

    private final NewsService newsService;

    /**
     * 查询资讯列表
     */
    @PreAuthorize("@ss.hasPermi('news:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(News news) {
        startPage();
        List<News> list = newsService.queryList(news);
        return getDataTable(list);
    }

    /**
     * 导出资讯列表
     */
    @PreAuthorize("@ss.hasPermi('news:news:export')")
    @Log(title = "资讯", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(News news) {
        List<News> list = newsService.queryList(news);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        return util.exportExcel(list, "news");
    }

    /**
     * 获取资讯详细信息
     */
    @PreAuthorize("@ss.hasPermi('news:news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(newsService.getById(id));
    }

    /**
     * 新增资讯
     */
    @PreAuthorize("@ss.hasPermi('news:news:add')")
    @Log(title = "资讯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody News news) {
        return toAjax(newsService.save(news) ? 1 : 0);
    }

    /**
     * 修改资讯
     */
    @PreAuthorize("@ss.hasPermi('news:news:edit')")
    @Log(title = "资讯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody News news) {
        return toAjax(newsService.updateById(news) ? 1 : 0);
    }

    /**
     * 删除资讯
     */
    @PreAuthorize("@ss.hasPermi('news:news:remove')")
    @Log(title = "资讯", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(newsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
